var ifSuperMan = window.localStorage.getItem("backstage-access-ifSuperMan");
var listData;

$(function() {

  if (ifSuperMan == 1) {
    listData = {
      "initData": { //默认参数配置
        "page": "1",
        "pageSize": "10"
      },
      "url": site.apiUrl + "/studentOrder/selectStudentOrderList", // 数据请求url
      "type": "get",
      // "checkname": "datachk", //选择器名称，为空不显示选择器
      "col": [ //数据列
        // { "field": "imgUrl", "display": "图片", "fun": "changePic", "width": "1" },
        { "field": "orderDay", "display": "预约时间", "fun": "ordeDateTime", "width": "3" },
        { "field": "name", "display": "申请人姓名" },
        { "field": "telephone", "display": "手机号" },
        { "field": "wxId", "display": "身份证号码", "width": "3" },
        {
          "field": "status",
          "display": "状态",
          "width": "1",
          "show": [ //数据自定义显示
            { "value": "0", "html": "<span>进行中</span>" },
            { "value": "1", "html": "<span>已取消</span>" },
            { "value": "2", "html": "<span>已完成</span>" }
          ]
        },
        { "field": "webName", "display": "服务网点", "width": "2" },
        { "field": "homeTown", "display": "乡镇名称" },
        {
          "field": "ifDel",
          "display": "是否删除",
          "width": "1",
          "show": [ //数据自定义显示
            { "value": "0", "html": "<span>未删除</span>" },
            { "value": "1", "html": "<span class='red'>已删除</span>" }
          ]
        },
        { "field": "createTime", "display": "创建时间", "fun": "changeDate", "width": "2" }
      ],
      "option": [ //操作按钮配置  icon: add, repartyorg, ogoto, edit, more, senior, knotitem, del
        // { "icon": "edit", "tip": "编辑", "url": "edit.html?type=2&id={{id}}" },
        { "icon": "del", "tip": "删除", "fun": "deleteBrand('{{id}}')" }
      ]
    }
  } else {
    listData = {
      "initData": {
        "page": "1",
        "pageSize": "10"
      },
      "url": site.apiUrl + "/studentOrder/selectStudentOrderList",
      "type": "get",
      "col": [
        { "field": "orderDay", "display": "预约时间", "fun": "ordeDateTime", "width": "3" },
        { "field": "name", "display": "申请人姓名" },
        { "field": "telephone", "display": "手机号" },
        {
          "field": "status",
          "display": "是否预约成功",
          "width": "1",
          "show": [ //数据自定义显示
            { "value": "0", "html": "<span>进行中</span>" },
            { "value": "1", "html": "<span>已取消</span>" },
            { "value": "2", "html": "<span>已完成</span>" }
          ]
        },
        { "field": "webName", "display": "服务网点", "width": "2" },
        { "field": "homeTown", "display": "乡镇名称" },
        { "field": "createTime", "display": "创建时间", "fun": "changeDate", "width": "2" }
      ]
    }
  }
  $("#nameList").getList(listData);

  // 导出表格
  // $("#exportExcel").click(function() {
  //   ly.confirm("确定导出吗？", function(r) {
  //     var startTime = $("#startTime").val();
  //     var endTime = $("#endTime").val();
  //     window.open(site.apiUrl + "/studentOrder/exportUserList?startTime=" + startTime + "&endTime=" + endTime);
  //   })
  // })

  //批量删除
  $("#batchDelBtn").click(function() {
    var c = "";
    $("input[name=datachk]:checked").each(function() {
      c += $(this).val() + ',';
    });
    c = c.substr(0, c.length - 1)
    if (c != null && c != '') {
      deleteBrand(c);
    } else {
      ly.toast("请选择删除项");
    }
  })

})

// 设置默认头像
function changePic(v, o, d) {
  if (d.imgUrl != null && d.imgUrl != '') {
    $(o).html("<img src=" + d.imgUrl + " width='30' height='30'>");
  } else {
    $(o).html("<img src='" + site.interUrl + "/img/default_head.jpg' width='30' height='30'>");
  }
}

// 预约时间
function ordeDateTime(v, o, d) {
  $(o).html('<span>'+ d.orderDay + ' ' + d.startTime + ' ~ ' + d.endTime +'</span>');
}

// 时间戳转换时间
function changeDate(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.createTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.createTime != null && d.createTime != '') {
    $(o).html(baseDateTime)
  }
}

// 导出EXCEL
function exportExcelWithJS(){
  // 表格数据是否为空
  var tableIsEmpty = $('#nameList table tr td').hasClass("nodata");
  console.log(tableIsEmpty)
  if (!tableIsEmpty) {
    ly.confirm("确定导出吗？", function(r) {
      $('#nameList table').tableExport({
        type:'excel',
        fileName:'预约名单',
      });
    })
  } else {
    ly.toast("数据为空，暂不导出")
  }
}

//删除
function deleteBrand(id) {
  ly.confirm("确定删除吗？", function(r) {
    var url = site.apiUrl + "/studentOrder/deleteStudentOrderInfo";
    request({
      url: url,
      data: { "id": id },
      type: "get",
      callback: function(f) {
        if (f.code == 200) {
          ly.toast("删除成功");
          $("#nameList").getList.refresh(); //刷新当前分页
        } else {
          ly.toast(f.desc || f.msg);
        }
      }
    })
  })
}